package com.oa.modules.approvalsec1.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.oa.common.utils.*;
import com.oa.modules.approvalsec1.dao.BiddProjectDao;
import com.oa.modules.approvalsec1.dao.InstitutionsDao;
import com.oa.modules.approvalsec1.dao.MattersDao;
import com.oa.modules.approvalsec1.entity.BiddProjectEntity;
import com.oa.modules.approvalsec1.entity.InstitutionsEntity;
import com.oa.modules.approvalsec1.entity.SmsEntity;
import com.oa.modules.approvalsec1.service.BiddProjectService;
import com.oa.modules.approvalsec1.service.JointsService;
import com.oa.modules.approvalsec1.service.MattersService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 事项
 *
 * @author Hankaicheng
 * @email 785297226@qq.com
 * @date 2022-05-05 15:23:15
 */
@Service("projectService")
@Log4j2
public class BiddProjectServiceImpl extends ServiceImpl<BiddProjectDao, BiddProjectEntity> implements BiddProjectService {

    @Autowired
    private InstitutionsDao institutionsDao;

    @Autowired
    private MattersService matterService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage page = AppUtils.getPage(new Query<BiddProjectEntity>(), params);
        return AppUtils.setPage(page, baseMapper.findPage(params), baseMapper.findRecords(page, params));
    }

    @Override
    @Async
    public void sendSms(BiddProjectEntity biddProject) {
        if (biddProject.getMode() == 2) {
            //  获取全部牵头单位法人电话
            String mobiles = StringUtils.join(institutionsDao.selectList(new QueryWrapper<InstitutionsEntity>().eq("take_lead_unit", 1)).stream().filter(e -> org.springframework.util.StringUtils.hasText(e.getContactPhone())).map(e -> e.getContactPhone()).collect(Collectors.toList()), ",");
            List<String> matters = matterService.getItemsByIdStr(biddProject.getServiceItems());
            if (StringUtils.isNotBlank(biddProject.getOtherMatter())) {
                matters.add(biddProject.getOtherMatter());
            }
            biddProject.setServiceItems(StringUtils.join(matters, ","));
            String content = String.format("中介联合体您好：%s 竞价中，请及时登陆“e齐办”联合体专区参与竞价。", biddProject.getServiceItems());
            SmsUtil.sendSms(mobiles, content);
        } else if (biddProject.getMode() == 1) {
            InstitutionsEntity institutionsEntity = institutionsDao.selectOne(new QueryWrapper<InstitutionsEntity>().eq("take_lead_unit", 1).eq("joint", biddProject.getJoint()));
            if (Objects.nonNull(institutionsEntity) && institutionsEntity.getContactPhone() != null) {
                String content = String.format("中介联合体您好：%s 竞价中，请及时登陆“e齐办”联合体专区参与竞价。", biddProject.getServiceItems());
                SmsUtil.sendSms(institutionsEntity.getContactPhone(), content);
            }
        }
    }




}
